package org.ruoyi.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.InterviewQuestion;
import org.ruoyi.interview.domain.InterviewQuestionFavorite;
import org.ruoyi.interview.domain.bo.InterviewQuestionFavoriteBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteCategoryVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionVo;
import org.ruoyi.interview.mapper.InterviewQuestionFavoriteMapper;
import org.ruoyi.interview.mapper.InterviewQuestionMapper;
import org.ruoyi.interview.service.IInterviewQuestionFavoriteService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 面试题收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-15
 */
@Service
@RequiredArgsConstructor
public class InterviewQuestionFavoriteServiceImpl implements IInterviewQuestionFavoriteService {

    private final InterviewQuestionFavoriteMapper baseMapper;
    private final InterviewQuestionMapper interviewQuestionMapper;

    /**
     * 查询面试题收藏
     *
     * @param favoriteId 收藏主键
     * @return 面试题收藏
     */
    @Override
    public InterviewQuestionFavoriteVo selectInterviewQuestionFavoriteById(Long favoriteId) {
        return baseMapper.selectVoById(favoriteId);
    }

    /**
     * 查询面试题收藏列表
     *
     * @param bo 面试题收藏业务对象
     * @return 面试题收藏集合
     */
    @Override
    public List<InterviewQuestionFavoriteVo> selectInterviewQuestionFavoriteList(InterviewQuestionFavoriteBo bo) {
        LambdaQueryWrapper<InterviewQuestionFavorite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询面试题收藏列表
     *
     * @param bo 面试题收藏业务对象
     * @param pageQuery 分页参数
     * @return 面试题收藏集合
     */
    @Override
    public TableDataInfo<InterviewQuestionFavoriteCategoryVo> selectInterviewQuestionFavoriteList(InterviewQuestionFavoriteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewQuestionFavorite> lqw = buildQueryWrapper(bo);
        IPage<InterviewQuestionFavoriteCategoryVo> page = baseMapper.selectFavoriteQuestionVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(page);
    }

    /**
     * 查询面试题收藏列表及题目详情
     *
     * @param bo 面试题收藏业务对象
     * @param pageQuery 分页参数
     * @return 面试题收藏集合
     */
    @Override
    public TableDataInfo<InterviewQuestionFavoriteCategoryVo> selectInterviewQuestionFavoriteQuestionList(InterviewQuestionFavoriteBo bo, PageQuery pageQuery) {
        // 使用链表SQL语句直接查询收藏及题目详情
        com.baomidou.mybatisplus.core.metadata.IPage<InterviewQuestionFavoriteCategoryVo> page = baseMapper.selectFavoriteQuestionVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(page);
    }

    /**
     * 新增面试题收藏
     *
     * @param bo 面试题收藏业务对象
     * @return 结果
     */
    @Override
    public Boolean insertInterviewQuestionFavorite(InterviewQuestionFavoriteBo bo) {
        // 检查是否已经收藏过该题目
        LambdaQueryWrapper<InterviewQuestionFavorite> checkWrapper = Wrappers.lambdaQuery();
        checkWrapper.eq(InterviewQuestionFavorite::getUserId, bo.getUserId());
        checkWrapper.eq(InterviewQuestionFavorite::getQuestionId, bo.getQuestionId());
        checkWrapper.eq(InterviewQuestionFavorite::getDelFlag, "0");
        InterviewQuestionFavorite existingFavorite = baseMapper.selectOne(checkWrapper);

        // 如果已经收藏过，直接返回true，避免重复收藏
        if (existingFavorite != null) {
            return true;
        }

        InterviewQuestionFavorite add = MapstructUtils.convert(bo, InterviewQuestionFavorite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFavoriteId(add.getFavoriteId());
        }
        return flag;
    }

    /**
     * 修改面试题收藏
     *
     * @param bo 面试题收藏业务对象
     * @return 结果
     */
    @Override
    public Boolean updateInterviewQuestionFavorite(InterviewQuestionFavoriteBo bo) {
        InterviewQuestionFavorite update = MapstructUtils.convert(bo, InterviewQuestionFavorite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除面试题收藏
     *
     * @param questionId 需要删除的收藏主键集合
     * @return 结果
     */
    @Override
    public Boolean deleteInterviewQuestionFavoriteByQuestionId(Long questionId) {
        LambdaQueryWrapper<InterviewQuestionFavorite> lqw = Wrappers.lambdaQuery();
        lqw.in(InterviewQuestionFavorite::getQuestionId, questionId);
        return baseMapper.delete(lqw)>0;
    }

    /**
     * 根据用户ID和题目ID查询收藏记录
     *
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 收藏记录
     */
    @Override
    public InterviewQuestionFavoriteVo selectInterviewQuestionFavoriteByUserIdAndQuestionId(Long userId, Long questionId) {
        LambdaQueryWrapper<InterviewQuestionFavorite> lqw = Wrappers.lambdaQuery();
        lqw.eq(InterviewQuestionFavorite::getUserId, userId);
        lqw.eq(InterviewQuestionFavorite::getQuestionId, questionId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 构建查询对象
     *
     * @param bo 面试题收藏业务对象
     * @return 查询对象
     */
    private LambdaQueryWrapper<InterviewQuestionFavorite> buildQueryWrapper(InterviewQuestionFavoriteBo bo) {
        LambdaQueryWrapper<InterviewQuestionFavorite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, InterviewQuestionFavorite::getUserId, bo.getUserId());
        lqw.eq(bo.getQuestionId() != null, InterviewQuestionFavorite::getQuestionId, bo.getQuestionId());
        lqw.eq(InterviewQuestionFavorite::getDelFlag, "0");
        return lqw;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InterviewQuestionFavorite entity) {
        // TODO 根据业务逻辑添加校验
    }
}
